package internact;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings({"all"})

/**
 * 会员管理模块
 */
public class MemberPanel extends JPanel {
    private JLabel usernameLabel;
    private JLabel passwordLabel;
    private JLabel nameLabel;
    private JLabel phoneLabel;
    private JLabel feeLabel;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JTextField nameField;
    private JTextField phoneField;
    private JTextField feeField;
    private JButton addButton;
    private JButton deleteButton;
    private JButton modifyButton;
    private MemberManager memberManager;

    public MemberPanel(MemberManager memberManager) {
        this.memberManager = memberManager;

        setLayout(new GridLayout(7, 2));

        usernameLabel = new JLabel("用户名：");
        add(usernameLabel);
        usernameField = new JTextField();
        add(usernameField);

        passwordLabel = new JLabel("密码：");
        add(passwordLabel);
        passwordField = new JPasswordField();
        add(passwordField);

        nameLabel = new JLabel("姓名：");
        add(nameLabel);
        nameField = new JTextField();
        add(nameField);

        phoneLabel = new JLabel("电话：");
        add(phoneLabel);
        phoneField = new JTextField();
        add(phoneField);

        feeLabel = new JLabel("会费：");
        add(feeLabel);
        feeField = new JTextField();
        add(feeField);

        addButton = new JButton("添加");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                String name = nameField.getText();
                String phone = phoneField.getText();
                double fee = Double.parseDouble(feeField.getText());
                if (memberManager.addMember(username, password, name, phone, fee)) {
                    JOptionPane.showMessageDialog(MemberPanel.this, "添加成功！");
                } else {
                    JOptionPane.showMessageDialog(MemberPanel.this, "添加失败！");
                }
            }
        });
        add(addButton);

        deleteButton = new JButton("删除");
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                if (memberManager.deleteMember(username)) {
                    JOptionPane.showMessageDialog(MemberPanel.this, "删除成功！");
                } else {
                    JOptionPane.showMessageDialog(MemberPanel.this, "删除失败！");
                }
            }
        });
        add(deleteButton);

        modifyButton = new JButton("修改");
        modifyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                String name = nameField.getText();
                String phone = phoneField.getText();
                double fee = Double.parseDouble(feeField.getText());
                if (memberManager.modifyMember(username, password, name, phone, fee)) {
                    JOptionPane.showMessageDialog(MemberPanel.this, "修改成功！");
                } else {
                    JOptionPane.showMessageDialog(MemberPanel.this, "修改失败！");
                }
            }
        });
        add(modifyButton);
    }
}

